﻿/**
 * Number.prototype.format(n, x, s, c)
 * local project use format(0, 3, ',', '.')
 * @param integer n: length of decimal
 * @param integer x: length of whole part
 * @param mixed   s: sections delimiter
 * @param mixed   c: decimal delimiter
 */

Number.prototype.format = function (n, x, s, c) {
    var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
        num = this.toFixed(Math.max(0, ~~n));

    return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
};


var Helper = (function ($) {
    return {
        isValidEmailAddress(emailAddress) {
            var pattern = /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i;
            return pattern.test(emailAddress);
        },
        setItem(key, value, ttl) {
            let item = {
                value: value,
                expiry: !!ttl ? ttl : (Date.now() + 90 * 24 * 60 * 60 * 1000)
            };
            localStorage.setItem(key, JSON.stringify(item));
        },

        getItem(key) {
            let item = localStorage.getItem(key);
            if (!item) return null;
            if (item.expiry && Date.now() > item.expiry) {
                localStorage.removeItem(key);
                return null;
            }
            item = JSON.parse(item);
            return item.value;
        },
        GetQueryString: function (key, url) {
            url = window.top.location.href;
            var regex = new RegExp("[?&]" + key + "(=([^&#]*)|&|#|$)", "i"), results = regex.exec(url);

            if (!results) {
                if (console) {
                    console.log('returning null for ' + key + ' and ' + url);
                }
                return null;
            }

            if (!results[2]) {
                if (console) {
                    console.log('returning empty for ' + key + ' and ' + url);
                }
                return '';
            }

            return decodeURIComponent(results[2].replace(/\+/g, " "));
        },
        setCookie(cname, cvalue, exdays) {
            exdays = exdays || -1;
            if (exdays === -1) {
                top.document.cookie = cname + "=" + cvalue + "; path=/";
                return;
            }
            var d = new Date();
            d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
            var expires = "expires=" + d.toUTCString();
            top.document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/";
        },
        getCookie(cname) {
            var name = cname + "=";
            var ca = top.document.cookie.split(";");
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == " ") c = c.substring(1);
                if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
            }
            return "";
        },
        isCompanyEmail(emailAddress) {
            var flag = true;
            emailAddress = emailAddress || "";
            const excludeEmailList = ['Gmail.com', 'Hotmail.com', 'yahoo.com', 'qq.com', '163.com', 'iCloud.com', 'outlook.com'];
            let isExclude = excludeEmailList.map(m => emailAddress.toLowerCase().indexOf(m.toLowerCase())).filter(q => q !== -1).length > 0;
            if (isExclude) {

                flag = false;
            }
            return flag;
        },
        isPC() {
            var userAgentInfo = navigator.userAgent;
            var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
            var flag = true;
            for (var v = 0; v < Agents.length; v++) {
                if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }
            }
            return flag;
        }

    };
})(jQuery);

// array of ISO YYYY-MM-DD format dates
var publicHolidays = {
    usa: [
        "2021/01/01",
        "2021/01/18",
        "2021/02/15",
        "2021/05/31",
        "2021/07/05",
        "2021/09/06",
        "2021/10/11",
        "2021/11/11",
        "2021/11/25",
        "2021/12/24",
        "2021/12/31",
    ],
};